package com.logos.musicframe.controller;

import com.logos.musicframe.entity.SongPlayHistory;
import com.logos.musicframe.resp.SongPlayHistoryResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.SongPlayHistoryService;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 * 播放历史(SongPlayHistory)表控制层
 *
 * @author logos
 * @since 2025-06-16 10:31:56
 */
@Component
@Slf4j
public class SongPlayHistoryController {
    /**
     * 服务对象
     */
    @Autowired
    private SongPlayHistoryService songPlayHistoryService;

    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 查询当前用户的播放历史
     */
    public Result<List<SongPlayHistoryResp>> query(){
        try {
            Integer userId = userInfoUtil.getUserId();
            List<SongPlayHistoryResp> songPlayHistoryRespList = songPlayHistoryService.queryByUser(userId);
            return Result.ok(songPlayHistoryRespList);
        }catch (Exception e){
            log.error("SongPlayHistoryController.query.error:{}",e.getMessage(),e);
            return Result.fail("查询播放历史失败");
        }
    }

    /**
     * 新增播放历史
     */
    public Result<Boolean> add(Integer songId){
        try {
            Integer userId = userInfoUtil.getUserId();
            Boolean result = songPlayHistoryService.add(userId, songId);
            return Result.ok(result);
        }catch (Exception e){
            log.error("SongPlayHistoryController.add.error:{}",e.getMessage(),e);
            return Result.fail("新增播放历史失败");
        }
    }

}

